package 抽象数据类型;

/**
 * @description: 无向边
 * @author: ywk
 * @date: 2021-08-11
 */
public class Edge implements Comparable<Edge> {
    protected final int v;//顶点之一
    protected final int w; //另一个顶点
    protected final double weight;//边的权重

    public Edge(int v, int w, double weight) {
        this.v = v;
        this.w = w;
        this.weight = weight;
    }

    public double weight() {
        return weight;
    }

    public int either() {
        return v;
    }

    public int other(int vertex) {
        if (vertex == v) {
            return w;
        } else if (vertex == w) {
            return v;
        }
        System.out.println("不存在的边");
        return Integer.MAX_VALUE;
    }

    @Override
    public int compareTo(Edge o) {
        return this.weight - o.weight > 0 ? 1 : this.weight - o.weight == 0 ? 0 : -1;
    }
    @Override
    public String toString() {
        return String.format("%d-%d %.2f", v, w, weight);
    }
}
